<template>
  <basic-container>
    <div v-show="showSearchFlag" class="flex-start m-t-20 m-b-10">
      <div class="flex-center">
        <el-date-picker v-model="searchDate" class="m-r-10" end-placeholder="结束日期" range-separator="至"
                        size="small"
                        start-placeholder="开始日期"
                        type="daterange"
                        value-format="yyyy-MM-dd"
        />
        <div class="m-r-10">
          <search-operate-type @select="search.logType=$event.ids"></search-operate-type>
        </div>
        <el-input v-model="search.deviceName" class="m-r-10" placeholder="请输入" size="small" style="width: 300px;"
                  @keyup.native.enter="query">
          <el-button slot="append" :loading="loading" icon="el-icon-search" size="small" type="primary" @click="query">
            查询
          </el-button>
        </el-input>
      </div>
    </div>
    <div class="flex-center m-b-10">
      <div></div>
      <div class="flex-center">
        <el-button :loading="loading" circle icon="el-icon-refresh" @click="query"></el-button>
        <el-button circle icon="el-icon-search" @click="showSearchFlag=!showSearchFlag"></el-button>
      </div>
    </div>
    <el-table v-loading="loading" :data="data" :header-cell-style="{background:'#eee',color:'#000'}" border
              class="my-table"
              style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="60"/>
      <el-table-column label="主板号" width="200">
        <template v-slot="scope">{{ scope.row.deviceName | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="scope">{{ (scope.row.logType  && $store.state.dict.dictCache.log_type[scope.row.logType ])| defaultValue }}</template>
      </el-table-column>
      <el-table-column label="时间">
        <template v-slot="scope">{{ scope.row.startTime | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="内容">
        <template v-slot="scope">{{ scope.row.content | defaultValue }}</template>
      </el-table-column>
    </el-table>
    <app-pagination ref="paginationRef" :page-size="page.size" :total="total" class="m-t-20" @change="pageChange"/>
    <div class="h-100px"></div>
  </basic-container>
</template>

<script>
import AppPagination from "@/components/business/app-pagination";
import {
  equals,
  getRecords,
  getTotal,
  isListEmpty,
  isStringEmpty,
  msgError,
  isTrue,
  confirmWarning, msgSuccess
} from "@/util/common-util";
import AuthWrapper from "@/components/business/auth-wrapper";
import request from "@/router/axios";
import {deleteRecord} from "@/api/business/filling-team";
import SearchProduct from "@/components/SearchCondition/SearchProduct";
import SearchProductGroup from "@/components/SearchCondition/SearchProductGroup";
import AppFileUpload from "@/components/business/app-file-upload";
import {iotlogPage} from "@/api/product/msgLog";
import SearchOperateType from "@/components/SearchCondition/SearchOperateType";



export default {
  name: "MsgList",
  components: {SearchOperateType, AppFileUpload, SearchProductGroup, SearchProduct, AuthWrapper, AppPagination},
  data() {
    return {
      page: {
        size: 20,
        current: 1
      },
      total: 0,
      searchDate: '',
      search: {
        deviceName: '',
        startTime: '',
        endTime: '',
        logType: '',
      },
      form: {
        name: '',
        contactName: '',
        contactPhone: '',
        baseUrl: '',
        authToken: ''
      },
      showSearchFlag: true,
      loading: false,
      data: [{}],
      showAddFlag: false,
      submitLoading: false,
      isEdit: false,
    }
  },
  mounted() {
    this.query()
  },
  filters: {
    teamStatus(v) {
      if (equals(v, 0)) return "冻结"
      if (equals(v, 1)) return "正常"
      return "未知"
    },
    userStatus(v) {
      if (equals(v, 0)) return "正常"
      if (equals(v, 1)) return "禁用"
      return "未知"
    }
  },
  methods: {
    handleUpload() {

    },
    pageChange(_page) {
      this.page = _page
      this.query()
    },
    refresh() {
      this.$refs.paginationRef.reset()
    },
    query() {
      this.search.startTime = this.searchDate && this.searchDate.length ? this.searchDate[0] + ' 00:00:00' : null
      this.search.endTime = this.searchDate && this.searchDate.length ? this.searchDate[1] + ' 23:59:59' : null
      this.loading = true
      iotlogPage({...this.page, ...this.search}).then(res => {
        this.data = getRecords(res)
        this.total = getTotal(res)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    addNew() {
      this.showAddFlag = true
    },
    doConfirm() {
      if (isStringEmpty(this.form.name)) return msgError("请输入主机厂名称")
      if (isStringEmpty(this.form.contactName)) return msgError("请输入主机厂联系人")
      if (isStringEmpty(this.form.contactPhone)) return msgError("请输入主机厂联系人方式")
      if (isStringEmpty(this.form.baseUrl)) return msgError("请输入同步链接")
      if (isStringEmpty(this.form.authToken)) return msgError("请输入主机厂token")
      this.submitLoading = true
      if (isTrue(this.isEdit)) {
        update(this.form).then(res => {
          this.submitLoading = false
          this.page.current = 1
          this.query()
          this.closeAddAndEdit()
        }).catch(() => {
          this.submitLoading = false
        })
      } else {
        save(this.form).then(res => {
          this.submitLoading = false
          this.page.current = 1
          this.query()
          this.closeAddAndEdit()
        }).catch(() => {
          this.submitLoading = false
        })
      }
    },
    toDetail(row) {
      this.$emit('detail', row)
    },
    closeAddAndEdit() {
      this.showAddFlag = false
      this.form.name = ''
      this.form.contactName = ''
      this.form.contactPhone = ''
      this.form.baseUrl = ''
      this.form.authToken = ''
      this.form.id = null
      this.isEdit = false
    },
    toRemove(row) {
      confirmWarning('删除操作', `确定删除主机厂 ${row.name} ？`, () => {
        remove(row.id).then(res => {
          this.query()
          msgSuccess()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.my-table {
  /deep/ .el-button--small {
    padding: 0 5px !important;
    font-size: 14px !important;
  }

  /deep/ td {
    padding: 8px 0 !important;
  }
}
</style>
